import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Calendar;

/**
 * 
 * Static class to evaluate user input and return formatted version
 * It's a static class
    */


public class DateValidation{

  
  private DateValidation()
  {
  }


/**
 * @param date string representation of a date in form of "dd/MM/yyyy"
 * @return Date instance of it parameter input.
 */
  public static Date validateDate(String date){
      Date validDate=null;
      try{
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			validDate = df.parse(date);
      }catch(ParseException e){System.err.println("Caught IOException: "
                        + e.getMessage()); }

      return validDate;
  }

/**
 * @param start end end two different instances of Date object
 * @return difference in days between two dates
 */
  public static int getPeriod(Date start,Date end){
      Calendar cal1=Calendar.getInstance();
	  Calendar cal2=Calendar.getInstance();
	  cal1.setTime(start);
	  cal2.setTime(end);
	  long d1=cal1.getTimeInMillis();
	  long d2=cal2.getTimeInMillis();
	  int diff=(int)((d2-d1)/1000/60/60/24) +1;
      return diff;
  }
  /**
   * 
   * ex for 340 minutes after midnight which represent 5:40 am, this method
   * returns 5
   * @param val time given in minutes after midnight
   * @return hour of parameter time
   */
  public static int getHour(int val){
      return val/60;
  }
/**
 * ex for 340 minutes after midnight which represent 5:40 am, this method
   * returns 40
 * @param val time given in minutes after midnight
 * @return hour of parameter time
 */
  public static int getMinuts(int val){
      return val % 60;
  }
/**
 * find closest monday, ex. if current system time is some date on thursday 19th
 * it will find
 * monday 23th
 * @return Calendar instance of closest monday from a current date
 */
  public static Calendar getMon(){
        Date d=new Date();
        Calendar c=Calendar.getInstance();
        c.setTime(d);
        int day=c.get(Calendar.DAY_OF_WEEK);
        if (day==Calendar.SUNDAY){
            c.add(Calendar.DAY_OF_WEEK, 1);
        }
        if (day==Calendar.MONDAY){
            c.add(Calendar.DAY_OF_WEEK, 7);
        }
        if (day==Calendar.TUESDAY){
            c.add(Calendar.DAY_OF_WEEK, 6);
        }
        if (day==Calendar.WEDNESDAY){
            c.add(Calendar.DAY_OF_WEEK, 5);
        }
        if (day==Calendar.THURSDAY){
            c.add(Calendar.DAY_OF_WEEK, 4);
        }
        if (day==Calendar.FRIDAY){
            c.add(Calendar.DAY_OF_WEEK, 3);
        }
        if (day==Calendar.SATURDAY){
            c.add(Calendar.DAY_OF_WEEK, 2);
        }
      return c;
  }
/**
 * find next date from a given one
 * @return next day from a given date
 * @param date Date instance when we want to obtain next day instance
 */
	public static Date nextDay(Date date){
        Calendar c=Calendar.getInstance();
	      c.setTime(date);
				c.add(Calendar.DAY_OF_WEEK, 1);
				return c.getTime();
	}


    public static int formatChosenTimeToInt(int hours, int minutes){
        return hours*60+minutes;
    }

}



